<template>
  <div class="app-container">
    <!-- 搜索头 -->
    <!-- <iframe
      class="iframe"
      src="http://114.215.158.79:8080/monitorSystemSigs/pages/login.jsp"
      frameborder="0"
      security="restricted"
      taget="_self"
    /> -->
    <div class="iframe">
      <div class="goto" @click="goto" />
    </div>
  </div>
</template>

<script>
import { page, del, update, add, versionUpdate, columns } from '@/api/device'
import { totals } from '@/api/total'
import { maintain } from '@/api/dashboard'
import { getFirmwareList } from '@/api/firmware'
import { toLowerCase } from '@/utils'
import { check } from '@/utils/check'
import { mapGetters } from 'vuex'

export default {
  filters: {
    onlineFilter: function(value) {
      const map = ['在线', '离线']
      return map[value]
    }
  },
  data() {
    return {
      list: [{
        name: 'xx单位',
        time: '2021/07/24/0:00',
        rename: '全站仪',
        num: '3',
        people: '张三',
        people1: '王五'
      },
      {
        name: 'xx单位',
        time: '2021/07/24/0:00',
        rename: '全站仪',
        num: '3',
        people: '张三',
        people1: '王五'
      },
      {
        name: 'xx单位',
        time: '2021/07/24/0:00',
        rename: '全站仪',
        num: '3',
        people: '张三',
        people1: '王五'
      },
      {
        name: 'xx单位',
        time: '2021/07/24/0:00',
        rename: '全站仪',
        num: '3',
        people: '张三',
        people1: '王五'
      },
      {
        name: 'xx单位',
        time: '2021/07/24/0:00',
        rename: '全站仪',
        num: '3',
        people: '张三',
        people1: '王五'
      }],
      listLoading: false,
      listQuery: {
        page: 1,
        size: 10,
        order: 'id',
        title: ''
      },
      total: 0,
      searchShow: false,
      formShow: false,
      isCreate: true, // true创建, false:更新
      headerShow: false,
      isIndeterminate: true,
      checkAll: true,
      columns: [],
      row: {},
      firmwareList: [],
      versionForm: {
        deviceId: '',
        fid: ''
      },
      rule: {
        fid: [{ required: true, message: '请选择版本', trigger: 'change' }]
      },
      firmwareShow: false,
      checkAdd: false,
      checkEdit: false,
      checkDelete: false,
      typeOptions: [
        {
          value: 0,
          label: '测距点'
        },
        {
          value: 1,
          label: '报警器'
        },
        {
          value: 2,
          label: '固定点'
        }
      ]

    }
  },
  computed: {
    // ...mapGetters([
    //   'roles'
    // ]),
    header: {
      set: function(v) {
        Object.keys(this.columns).forEach(key => {
          this.columns[key].show = false
          v.forEach(i => {
            if (key === i) {
              this.columns[key].show = true
            }
          })
        })
        const checkedCount = v.length
        this.checkAll = checkedCount === Object.keys(this.columns).length
        console.log(this.checkAll)
        this.isIndeterminate =
          checkedCount > 0 && checkedCount < Object.keys(this.columns).length
        console.log(
          this.isIndeterminate,
          v.length,
          Object.keys(this.columns).length
        )
        return Object.keys(this.columns).filter(i => this.columns[i].show)
      },
      get: function() {
        var array = Object.keys(this.columns).filter(i => this.columns[i].show)
        return array
      }
    }
  },
  mounted() {
    // check(this.roles, ['激活设备', '编辑设备', '删除设备'], (list) => {
    //   this.checkAdd = list[0]
    //   this.checkEdit = list[1]
    //   this.checkDelete = list[2]
    // })
    // this.fetchData()
    this.columns = totals()
    var checkedCount = this.header.length
    this.checkAll = checkedCount === Object.keys(this.columns).length
    this.isIndeterminate =
      checkedCount > 0 && checkedCount < Object.keys(this.columns).length
  },
  methods: {
    goto() {
      window.open('http://114.215.158.79:8080/monitorSystemSigs/pages/login.jsp?login_user=XZDGYZJC&login_pass=12345678', '_blank')
    },
    fetchData() {
      this.listLoading = true
      page(this.listQuery).then(response => {
        this.list = response.data.records.map(item => {
          const ccidData = item.ccidData ? item.ccidData === 'Error request, response status: 405' ? '' : JSON.parse(item.ccidData) : ''
          return {
            ...item,
            ll: ccidData
              ? ccidData.status === '0'
                ? `${ccidData.data.left_flow} || ${ccidData.data.total_flow}`
                : ''
              : ''
          }
        })
        this.total = response.data.total
        this.listLoading = false
      })
    },

    // <<<< 搜索头
    handleSearchClick() {
      if (!this.listQuery.title) {
        this.$message({
          message: '搜索内容不能为空',
          type: 'error',
          duration: 800
        })
      }
    },
    handleSearchCleanClick() {
      this.listQuery.title = ''
    },
    // >>>> 搜索头

    // <<<< 工具栏相关
    handleCheckAllChange(val) {
      this.header = val ? Object.keys(this.columns) : []
      Object.keys(this.columns).forEach(key => {
        this.columns[key].show = val
      })
      this.isIndeterminate = false
    },
    handleRefreshClick() {
      this.$message({
        message: '刷新成功',
        type: 'success',
        duration: 800
      })
      this.fetchData()
    },
    handleSearchShowChange() {
      this.searchShow = !this.searchShow
    },
    handleAddClick() {
      this.isCreate = true
      this.formShow = true
      this.row = {
        deflectionX: 0,
        deflectionY: 0,
        deflectionZ: 0
      }
    },
    // 升级版本
    handleUpdate(data) {
      getFirmwareList({ deviceId: data.id }).then(res => {
        if (res.ok) {
          this.firmwareList = res.data
        }
      })
      this.versionForm = {
        deviceId: data.id,
        fid: data.fid
      }
      this.firmwareShow = true
    },
    handleClickUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const { deviceId, fid } = this.versionForm
          versionUpdate({
            deviceId,
            fid
          }).then(res => {
            if (res.ok) {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 800
              })
              this.firmwareShow = false
              this.fetchData()
            }
          })
        }
      })
    },
    // 维护模式的开启与关闭 1 关闭状态 0 开启状态
    clickMaintain(data) {
      maintain({
        deviceId: data.id,
        status: data.isMain ? 0 : 1
      }).then(res => {
        if (res.ok) {
          this.$message({
            type: 'success',
            message: res.data
          })
          this.fetchData()
        }
      })
    },
    // >>>> 工具栏相关

    // <<<< 表头相关

    // >>>> 表头相关

    // <<<< 表格相关
    handleDeleteClick(row) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del(row.id).then(res => {
            this.fetchData()
          })
        })
        .catch(() => {
          console.log('取消删除')
        })
    },
    handleEditClick(row) {
      this.isCreate = false
      this.formShow = true
      this.row = Object.assign({}, row)
    },
    handleBeforeClose() {
      this.row = {}
      this.formShow = false
      this.$refs.form.resetFields()
    },
    // >>>> 表格相关

    // <<<< 分页相关
    handleSizeChange(val) {
      console.log(val)
      this.listQuery.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },
    handleSortChange(val) {
      this.listQuery.asc = val.order !== 'descending'
      this.listQuery.order = toLowerCase(val.prop)
      this.fetchData()
    },
    handleHeaderClick() {
      this.headerShow = true
    },
    // >>>> 分页相关

    // <<<< 表单相关
    handleCreateClick() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.row.x = this.row.x ? this.row.x : 0
          this.row.y = this.row.y ? this.row.y : 0
          this.row.z = this.row.z ? this.row.z : 0
          add(this.row).then(res => {
            this.$message({
              message: res.data,
              type: 'success',
              duration: 800
            })
            this.fetchData()
            this.formShow = false
            this.form = {}
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleUpdateClick() {
      this.$refs.form.validate(valid => {
        if (valid) {
          update(this.row).then(res => {
            this.fetchData()
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 800
            })
            this.formShow = false
            this.form = {}
          })
        }
      })
    }
    // >>>> 表单相关

    // <<<<< 其他业务
    // >>>>> 其他业务
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  min-height: calc(100vh - 50px);
  width: 100%;
  position: relative;
}
.iframe {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  min-height: calc(100vh - 50px);
  background-image: url("~@/assets/pic/bg.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: 100%;

  .goto {
    position: absolute;
    right: 279.15px;
    top: 47%;
    width: 288px;
    height: 70px;
    border: 0 none;
    margin-left: 470px;
    margin-top: 25px;
    vertical-align: middle;
    line-height: 70px;
    text-align: center;
    color: #fff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    padding-left: 4px;
    background-image: url("~@/assets/pic/yanzheng.png");
    background-size: 100% 100%;
    cursor: pointer;
  }
}
</style>
